<?
require_once("framework/include.php");
class Home extends Module 
{
	var $page_title = "Home";
	
	function init()
	{
		// create user database
		// if you want to reset the users: $this->db->users->drop();
		$this->db->users->ensureIndex(array('email'=>1),array('unique'=>true));
		// init admin if not found
		if (!$this->db->users->findOne(array('email'=>'admin')))
			$this->db->users->save(array('email'=>'admin','p'=>md5('abc123')));
	}
	
	function _default()
	{
		$this->display('home.tpl');
	}
	
	function login()
	{
		if ($this->login) { $this->redir(URL_BASE_HREF); exit; }
		
		$this->page_title = 'Login';
		
		if (isset($_POST['e']) && isset($_POST['p']))
		{
			// validate
			$ret = $this->db->users->findOne(array('email'=>$_POST['e'],'p'=>md5($_POST['p'])));
			if ($ret) 
			{
				$this->db->users->update(array('_id'=>$ret['_id']),array('$set'=>array('last_login'=>new MongoDate())));
				$this->set_login($ret);
				if (isset($_REQUEST['redir']))
					$this->redir($_REQUEST['redir']);
				else
					$this->redir(URL_BASE_HREF);
				exit;
			}
			$this->login_err = 'Invalid Email or Password';
		}
		
		$this->display("login.tpl");
	}
		
	function logout()
	{
		$this->page_title = "Logout";
		$this->unset_login();
		$this->display("logout.tpl");
	}

	function signup()
	{
		$this->page_title = "Signup";
		$this->display("signup.tpl");
	}

}

new Home();
?>